package com.scientificInfo.server;

import com.scientificInfo.bean.*;
import com.scientificInfo.dao.*;
//import javafx.scene.shape.Circle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

@Service
public class TeacherServer {
    @Autowired
    PatentMapper patentMapper;
    @Autowired
    PaperMapper paperMapper;
    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    MessageMapper messageMapper;
    @Autowired
    WorksMapper worksMapper;
    @Autowired
    MeetingMapper meetingMapper;
    @Autowired
    SourceMapper sourceMapper;
    @Autowired
    TakeMeetingMapper takeMeetingMapper;

    /**
     * 查询所有系统消息
     *
     * @return 返回所有消息。
     */
    public List<Message> selectSystemMsg() {
        MessageExample messageExample = new MessageExample();
        MessageExample.Criteria criteria = messageExample.createCriteria();
        criteria.andTouserEqualTo("allTeacher");  //给所有教师的信息
        //criteria.andBeginLessThan(20190611);
        //criteria.andEndGreaterThan(20191212);
        return messageMapper.selectByExample(messageExample);
    }

    /**
     * 查询某个教师的私信
     *
     * @param teacherId 要查询的教师的Id
     * @return 返回这个教师的私信列表
     */
    public List<Message> selectMsgByTeacherId(String teacherId) {
        MessageExample messageExample = new MessageExample();
        MessageExample.Criteria criteria = messageExample.createCriteria();
        criteria.andTouserEqualTo(teacherId);
//        criteria.andBeginLessThan(20190611);
//        criteria.andEndGreaterThanOrEqualTo(20191212);
        return messageMapper.selectByExample(messageExample);
    }

    /**
     * 根据教师Id 选择指定状态下的论文
     */
    public List<Paper> selectPaperByTeacherId(int ope, String teacherId) {
        PaperExample paperExample = new PaperExample();
        PaperExample.Criteria criteria = paperExample.createCriteria();
        criteria.andTeacheridEqualTo(teacherId);
        criteria.andCheckflagEqualTo(ope);
        return paperMapper.selectByExample(paperExample);
    }

    /**
     * 根据教师Id 查询制定状态下的 专利
     *
     * @param ope       要查询专利的状态
     * @param teacherId 教师Id
     * @return
     */
    public List<Patent> selectPatent(int ope, String teacherId) {
        PatentExample patentExample = new PatentExample();
        PatentExample.Criteria circle = patentExample.createCriteria();
        circle.andTeacheridEqualTo(teacherId);
        circle.andCheckflagEqualTo(ope);
        return patentMapper.selectByExample(patentExample);
    }

    /**
     * 查询指定教师，指定状态下的项目的信息。
     *
     * @param ope       项目状态
     * @param teacherId 教师Id
     * @return
     */
    public List<Project> selectProject(int ope, String teacherId) {
        ProjectExample projectExample = new ProjectExample();
        ProjectExample.Criteria circle = projectExample.createCriteria();
        circle.andTeacheridEqualTo(teacherId);
        circle.andCheckflagEqualTo(ope);
        return projectMapper.selectByExample(projectExample);
    }

    @Transactional
    public int insertPaper(Paper paper) {
        //添加论文
        int codeInsert = paperMapper.insertPart(paper);

        //根据论文类型自动评分
        int codeScore = paperMapper.updatePaperScoreByType(paper.getResultuuid(), paper.getPapertypeid());

        return codeScore + codeInsert;
    }

    @Transactional
    public int insertPatent(Patent patent) {
        int codeInsert = patentMapper.insert(patent);

        //根据论文类型自动评分
        int codeScore = patentMapper.updatePatentScoreByType(patent.getResultuuid(), patent.getPatenttype());

        return codeInsert + codeScore;
    }


    //根据项目类型 自动评分
    @Transactional
    public void insertProject(Project project) {
        projectMapper.insert(project);

    }

    public List<Teacher> selectTeacherById(String userId) {
        TeacherExample teacherExample = new TeacherExample();
        TeacherExample.Criteria criteria = teacherExample.createCriteria();
        criteria.andTeacheridEqualTo(userId);
        return teacherMapper.selectByExample(teacherExample);

    }

    public void updateTeacher(Teacher teacher) {
        //int record = teacherMapper.update(teacher);
        teacherMapper.updateByPrimaryKeySelective(teacher);
    }

    public int insertWorks(Works works) {
        return worksMapper.insert(works);
    }

    public List<Meeting> selectAllMeeting() {
        return meetingMapper.selectAll();
    }

    public int insertMeeting(Meeting meeting) {
        return meetingMapper.insertOne(meeting);
    }

    public Meeting selectOneMeeting(String meetingname) {
        return meetingMapper.selectOneByName(meetingname);
    }

    public int insertTeacher(Teacher teacher) {
        return teacherMapper.insert(teacher);
    }

    public int updateTeacherMeetingById(String teacher_id, int newMeetingId) {
        return teacherMapper.updateByOne(teacher_id, newMeetingId);
    }

    public List<Teacher> getNameByMeetingId(int meetingid) {
        return teacherMapper.selectByMeetingId(meetingid);
    }

    public int saveFilePath(String filePathSaved, String fileName) {

        return sourceMapper.saveFilePath(filePathSaved, fileName);
    }

    public int takeMeeting(int meetingid, String teacherId) {
        int data = 2;
        //1. 是否该教师已经参加该会议
        TakeMeeting takeMeeting = takeMeetingMapper.selectOne(meetingid, teacherId);
        if(takeMeeting != null)
            return data;
        data = takeMeetingMapper.insertOne(meetingid, teacherId);
        //2.联系表中插入会议与教师的信息
        return data;
    }

    public List<TakeMeeting> selectByMeetingId(Integer meetingid) {
        return takeMeetingMapper.selectByMeetingId(meetingid);
    }
}
